/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.metasketch.model.loader;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author guilhermegrg
 */
public class InMemoryGraphDatabase implements IGraphDatabase {

	// LinkProviderManager linkManager = new LinkProviderManager();
	Map<String, GraphNode> nodesPerId = new HashMap<String, GraphNode>();
	List<GraphNode> nodes = new ArrayList<GraphNode>();

	// boolean active = false;

	@Override
	public void storeNode(GraphNode node) {
		if (nodesPerId.keySet().contains(node.getId())) {
			System.err.println("Duplicate! Node with id " + node.getId()
					+ " already exists!");
			return;
		}

		nodesPerId.put(node.getId(), node);
		nodes.add(node);
	}

	// @Override
	// public void storeRelationship(String type, Map<String, String>
	// properties,
	// String sourceId, String targetId) {
	// GraphRelationship relationship = new GraphRelationship(null, type);
	// relationship.setSourceId(sourceId);
	// relationship.setTargetId(targetId);
	// if (properties != null) {
	// relationship.getProperties().putAll(properties);
	// }
	// storeRelationship(relationship);
	// }
	//
	// @Override
	// public void storeRelationship(GraphRelationship relationship) {
	// GraphNode sourceNode = getSingleById(relationship.getSourceId());
	// GraphNode targetNode = getSingleById(relationship.getTargetId());
	//
	// if (sourceNode == null || targetNode == null) {
	// System.err
	// .println("Relation error! Didn't found source/target with id: "
	// + relationship.getSourceId()
	// + " - "
	// + relationship.getTargetId());
	// return;
	// }
	//
	// relationship.setSource(sourceNode);
	// relationship.setTarget(targetNode);
	//
	// sourceNode.addRelationship(relationship);
	// targetNode.addRelationship(relationship);
	//
	// // sourceNode.getSourceRelationships().add(relationship);
	// // targetNode.getTargetRelationships().add(relationship);
	// //
	// //
	// // sourceNode.getRelationships().add(relationship);
	// // targetNode.getRelationships().add(relationship);
	//
	// }

	@Override
	public GraphNode getById(String id) {
		return nodesPerId.get(id);
	}

	@Override
	public GraphNode getSingleByName(String name) {
		for (GraphNode node : nodes) {
			if (node.getName().contains(name)) {
				return node;
			}
		}
		return null;
	}

	@Override
	public Collection<GraphNode> getNodes() {
		return nodes;
	}

	// @Override
	// public Collection<GraphNode> getLinks(GraphNode node, String type) {
	// return linkManager.getLinks(node, type, this);
	// }

	@Override
	public Collection<GraphNode> getByName(String name) {
		List<GraphNode> found = new ArrayList<GraphNode>();
		for (GraphNode node : nodes) {
			if (node.getName().contains(name)) {
				found.add(node);
			}
		}
		return found;
	}

	// @Override
	// public Collection<GraphNode> getSeveralNodesByProperty(String name,
	// String value) {
	// throw new UnsupportedOperationException("Not supported yet.");
	// }

	@Override
	public Collection<GraphNode> getByType(String name) {
		List<GraphNode> found = new ArrayList<GraphNode>();
		for (GraphNode node : nodes) {
			if (node.getType().contains(name)) {
				found.add(node);
			}
		}
		return found;
	}
}
